import {create} from 'zustand'
import {
    usersFriendRequestsDataType,
    usersRelationships,
    usersRelationshipsDataType,
    usersFriendRequests
} from "@src/services/userinfo.ts"
import {messageType} from "@src/pages/home/news/privateChat/index.tsx"

interface myFriendRequestNewMessageType extends messageType {
    sum: number
}

interface newsDataCreateType {
    //我发送的好友申请
    myFriendRequest0?: usersRelationshipsDataType[],
    //好友列表
    myFriendRequest1?: usersRelationshipsDataType[],
    //待验证好友申请
    friendRequest0?: usersFriendRequestsDataType[],
    //已通过好友申请
    friendRequest1?: usersFriendRequestsDataType[],
    //更新 我发送的好友申请 好友列表
    updateMyFriendRequest: () => void,
    //更新 待验证好友申请 已通过好友申请
    updateFriendRequest: () => void,
    //通过 好友 验证
    successFriend: (item: usersFriendRequestsDataType) => void,
    //新发送的 待验证好友申请
    newFriendRequestLength: number,
    setNewFriendRequestLength: (newFriendRequestLength: number) => void,
    //新私信消息
    newMessageList: messageType[],
    AddNewMessageList: (message: messageType) => void,
    myFriendRequestNewMessage?: Array<myFriendRequestNewMessageType | undefined>,
    //根据id 消息 设置 为已读
    readMessage: (id: number) => void,
}

export const newsDataCreate = create<newsDataCreateType>((set, get) => ({
    myFriendRequest0: undefined,
    myFriendRequest1: undefined,
    friendRequest0: undefined,
    friendRequest1: undefined,
    newFriendRequestLength: 0,
    myFriendRequestNewMessage: undefined,
    updateMyFriendRequest: () => {
        usersRelationships().then((relationships) => {
            const Request0 = relationships.data.filter(item => item.relationship == "0")
            const Request1 = relationships.data.filter(item => item.relationship == "1")
            set({myFriendRequest0: Request0, myFriendRequest1: Request1})
        })
    },
    updateFriendRequest: () => {
        usersFriendRequests().then((relationships) => {
            const Request0 = relationships.data.filter(item => item.relationship == "0")
            const Request1 = relationships.data.filter(item => item.relationship == "1")
            set({friendRequest0: Request0, friendRequest1: Request1})
        })
    },
    successFriend: (item: usersFriendRequestsDataType) => {
        const {friendRequest0, friendRequest1} = get();
        const friend = friendRequest0?.find(i => i.user_id === item.user_id)
        let text0: usersFriendRequestsDataType[] | undefined = []
        if (friend) {
            text0 = friendRequest0?.filter(item1 => item1.user_id != friend?.user_id)
            friendRequest1?.push(friend)
        }
        set({
            friendRequest0: text0,
            friendRequest1: friendRequest1
        })
    },
    setNewFriendRequestLength: (newFriendRequestLength: number) => {
        set({newFriendRequestLength: newFriendRequestLength})
    },
    newMessageList: [],
    AddNewMessageList: (message: messageType) => {
        const {newMessageList, myFriendRequest1} = get()
        newMessageList.unshift(message)
        const sum = newMessageList.filter(item => item.id == message.id).length
        const newData: messageType[] = newMessageList.filter(item => item.id !== message.id)
        newData.push(message)
        const myFriendNewMessage: Array<myFriendRequestNewMessageType | undefined> = []
        if (myFriendRequest1) {
            myFriendRequest1.forEach(item => {
                const item2 = newData.find(item3 => item3.id == item.friend_id)
                if (item2) {
                    myFriendNewMessage.push({...item2, sum: sum})
                } else {
                    myFriendNewMessage.push(undefined)
                }

            })
        }
        set({newMessageList: newMessageList, myFriendRequestNewMessage: myFriendNewMessage})
    },
    readMessage: (id: number) => {
        const {newMessageList, myFriendRequestNewMessage} = get()
        const newData: messageType[] = newMessageList.filter(item => item.id !== id)
        const newMyFriendRequestNewMessage = myFriendRequestNewMessage?.filter(item => item?.id != id)
        set({newMessageList: newData, myFriendRequestNewMessage: newMyFriendRequestNewMessage})
    }
}))